﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Baseframework.WebUI;
using System.Data;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;
using Honestysoft.Financing.WebUI.Module.Audit;

namespace Honestysoft.Financing.WebUI.Module.Reports
{
    public partial class BatchReport : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                Bind();
                BindReport();
            }
        }

        private void Bind()
        { 
            QueryParameters query = new QueryParameters();
            OrderParameters order = new OrderParameters();
            order.Add("BatchNo", OrderParameters.OrderDirection.Desc);
            DataTable dt = new BaseBusinessLogic<Bu_AuditBatch>(Globals.CurrentConnStringName).GetTable(query, order);
            ddlBatchNo.DataSource = dt;
            ddlBatchNo.DataTextField = "BatchNo";
            ddlBatchNo.DataValueField = "ID";
            ddlBatchNo.DataBind();
            if (ddlBatchNo.Items.Count > 0)
            {
                ddlBatchNo.SelectedIndex = 0;
            }
        }


        private void BindReport()
        {
            BaseBusinessLogic<Bu_BatchReport> bll = new BaseBusinessLogic<Bu_BatchReport>(Globals.CurrentConnStringName);
            Bu_BatchReport report =bll.GetEntity(ddlBatchNo.SelectedValue);
            if (report == null)
            {
                if (!string.IsNullOrEmpty(ddlBatchNo.SelectedValue))
                {
                    report = new Bu_BatchReport();
                    report.DocNo = "未设置";
                    report.CreateTime = DateTime.Now;
                    report.DocDate = report.CreateTime;
                    report.Creator = UserLoginer.LoginUser.ID;
                    report.CreatorName = UserLoginer.LoginUser.Name;
                    report.DocUrl = "Reports/" + ddlBatchNo.SelectedItem.Text + "/" + ddlBatchNo.SelectedItem.Text + "关于市政府融资项目拨款审核工作的情况报告第" + ChineseNum.GetChineseNum(ddlBatchNo.SelectedItem.Text) + "批.doc";
                    report.ID = ddlBatchNo.SelectedValue;
                    bll.CreateEntity(report);
                }
            }
            if (report == null) { return; }
            txtDocNo.Text = report.DocNo;
            dtpDocDate.Text = report.DocDate.ToString("yyyy-MM-dd");
            QueryParameters query = new QueryParameters();
            query.Add("BatchID",report.ID, QueryParameters.OperatorType.Equal);
            OrderParameters order = new OrderParameters();
            order.Add("Code");
            DataTable dt = new BaseBusinessLogic<Bu_AuditTable>(Globals.CurrentConnStringName).GetTable(query, order);
            rptList.DataSource = dt;
            rptList.DataBind();
            List<string> lsOwner=new List<string>();
            List<string> lsProject=new List<string>();
            List<string> lsContract=new List<string>();
            decimal sumDocRec=0,sumAmountS=0;
            foreach(DataRow dr in dt.Rows)
            {
                if(!lsOwner.Contains(dr["Owner"].ToString())){lsOwner.Add(dr["Owner"].ToString());}
                if(!lsProject.Contains(dr["ProjectID"].ToString())){lsProject.Add(dr["ProjectID"].ToString());}
                if(!lsContract.Contains(dr["ContractID"].ToString())){lsContract.Add(dr["ContractID"].ToString());}
                sumDocRec += Convert.ToDecimal(dr["Amount_DocRec_S"]);
                sumAmountS += Convert.ToDecimal(dr["Amount_S"]);
            }
            lblBatch.Text = ChineseNum.GetChineseNum(ddlBatchNo.SelectedItem.Text);
            lblCode.Text = dt.Rows.Count.ToString();
            lblContract.Text = lsContract.Count.ToString();
            lblOwner.Text = lsOwner.Count.ToString();
            lblProject.Text = lsProject.Count.ToString();
            lblSumDocRec.Text = string.Format("{0:N}", sumDocRec);
            lblSumAmountS.Text = string.Format("{0:N}", sumAmountS);
        }

        protected void ddlBatchNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindReport();
        }
    }
}